package jerry.html;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import jerry.html.events.EvtCap;

/**
 * 
 * Converts logs to objects for quick parsing.
 * 
 * 
 * @author (h0t@_G0|i
 * 
 */
public class LogParser {
	public static final ResourceBundle classes = ResourceBundle.getBundle("logger");
	private Map<String, List<EvtCap>> allEvents = new HashMap<String, List<EvtCap>>();
	
	@SuppressWarnings("unchecked")
	public void parse(File f) throws Exception {
		BufferedReader fileReader = new BufferedReader(new FileReader(f));
		String line = null;
		while(null != (line = fileReader.readLine())) {
			String[] peices = line.split("\t");
			Class<EvtCap> c = null;
			try{
				String className = classes.getString(peices[1]);
				c = (Class<EvtCap>)Class.forName(className);
			} catch(Exception cnfe) {
				System.err.println("class not found, entry " + peices[1] + " discarded");
				continue;
			}
			EvtCap event = (EvtCap) c.getConstructors()[0].newInstance(Arrays.asList(peices));
			if(event.isEvtValid()) {
				List<EvtCap> list = allEvents.get(peices[1]);
				if(list == null) {
					list = new ArrayList<EvtCap>();
					allEvents.put(peices[1], list);
				}
				list.add(event);
			} else {
				System.err.println("rejecting string : " + line);
			}
		}
	}
	
	public Map<String, List<EvtCap>> getAllEvents() {
		return allEvents;
	}
	
	public static void main(String[] args) throws Exception {
		File f = new File("src/Unreal.ngLog.2001.03.15.00.17.40.7777.log");
		new LogParser().parse(f);
		f = new File("src/Unreal.ngLog.2010.07.24.12.11.30.7777.log.temp");
		new LogParser().parse(f);
	}
}